package speedchina.service.statistics.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import speedchina.dao.statistics.StatisticsMapper;
import speedchina.dto.PageParamDTO;
import speedchina.dto.ProjectStatisticsParamDTO;
import speedchina.dto.UserStatisticsParamDTO;
import speedchina.service.statistics.StatisticsService;
import speedchina.util.DateUtils;
import speedchina.vo.statistics.ProjectDetailVO;
import speedchina.vo.statistics.ProjectStatisicsVO;
import speedchina.vo.statistics.UserDetailVO;
import speedchina.vo.statistics.UserStatisicsVO;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author earthchen
 * @date 2018/10/19
 **/
@Service
public class StatisticsServiceImpl implements StatisticsService
{

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public IPage<ProjectStatisicsVO> projectStatistics(ProjectStatisticsParamDTO dto,
                                                       PageParamDTO pageParamDTO)
    {
        Page<ProjectStatisicsVO> page = new Page<>(pageParamDTO.getPage(), pageParamDTO.getSize());
        return statisticsMapper.projectStatistics(page, dto);
    }

    @Override
    public List<ProjectStatisicsVO> projectStatistics(ProjectStatisticsParamDTO dto)
    {
        return statisticsMapper.projectStatistics(dto);
    }

    @Override
    public IPage<UserStatisicsVO> userStatistics(UserStatisticsParamDTO dto,
                                                 PageParamDTO pageParamDTO)
    {
        Double shouldHour=7.5 * (DateUtils.getDayBetween(dto.getStartDate(), dto.getEndDate()) + 1 -
                                 dto.getNum());
        dto.setShouldHour(shouldHour);
        Page<UserStatisicsVO> page = new Page<>(pageParamDTO.getPage(), pageParamDTO.getSize());
        IPage<UserStatisicsVO> userStatisicsVOS = statisticsMapper.userStatistics(page, dto);
        userStatisicsVOS.getRecords().forEach(u -> {
            // 计算应有工时
            u.setShouldHour(
                7.5 * (DateUtils.getDayBetween(dto.getStartDate(), dto.getEndDate()) + 1 -
                       dto.getNum()));
        });
        return userStatisicsVOS;
    }

    @Override
    public List<UserStatisicsVO> userStatistics(UserStatisticsParamDTO dto)
    {
        List<UserStatisicsVO> userStatisicsVOS = statisticsMapper.userStatistics(dto);
        filter(userStatisicsVOS, dto);
        return userStatisicsVOS;
    }

    /**
     * 过滤
     *
     * @param userStatisicsVOS
     * @param dto
     */
    private void filter(List<UserStatisicsVO> userStatisicsVOS, UserStatisticsParamDTO dto)
    {
        userStatisicsVOS.forEach(u -> {
            // 计算应有工时
            u.setShouldHour(
                7.5 * (DateUtils.getDayBetween(dto.getStartDate(), dto.getEndDate()) + 1 -
                       dto.getNum()));
        });

        // 判断状态过滤是否异常
        if ("0".equals(dto.getStatus()) )
        {
            userStatisicsVOS = userStatisicsVOS.stream().filter( u -> u.getShouldHour().equals(u.getWorkhour())).collect(Collectors.toList());
        }
        else if ("1".equals(dto.getStatus()))
        {
            userStatisicsVOS = userStatisicsVOS.stream().filter(u -> u.getShouldHour() > u.getWorkhour()).collect( Collectors.toList());
        }
    }

    @Override
    public IPage<UserDetailVO> userDetail(UserStatisticsParamDTO dto, PageParamDTO pageParamDTO)
    {
        Page<UserDetailVO> page = new Page<>(pageParamDTO.getPage(), pageParamDTO.getSize());
        return statisticsMapper.userDetail(page, dto);
    }

    @Override
    public IPage<ProjectDetailVO> projectDetail(ProjectStatisticsParamDTO dto,
                                                PageParamDTO pageParamDTO)
    {
        Page<ProjectDetailVO> page = new Page<>(pageParamDTO.getPage(), pageParamDTO.getSize());
        return statisticsMapper.projectDetail(page, dto);
    }
}
